/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/

#ifndef _IPC_TRANS_MSG_MGT_H
#define _IPC_TRANS_MSG_MGT_H

#include "bst_ipc_config.h"
#include "ipc_serdes.h"
#include "ipc_trans_config.h"
#include "ipc_trans_lflist.h"

#ifdef __cplusplus
extern "C" {
#endif

struct _msg_object_t {
	rw_msg_t msg;
	uint64_t timestamp;
	uint16_t err_cnt;
	uint8_t res[6];
};
#define msg_object_t struct _msg_object_t

struct _packet_object_t {
	lflist_t packed_list;
	uint64_t mask;
	uint8_t completed_flag;
	uint8_t last_idx;
	uint16_t err_cnt;
	uint8_t res[4];
};
#define packet_object_t struct _packet_object_t

struct _bst_msg_queue_t {
	packet_object_t packet_obj[SESSION_PACKET_MSG_BUFFER_COUNT];
	msg_object_t msg_obj[SESSION_MSG_BUFFER_COUNT];
	lflist_node_t msg_nodes[SESSION_MSG_BUFFER_COUNT];
	lflist_node_t packet_nodes[SESSION_PACKET_MSG_BUFFER_COUNT];
	lflist_t free_list;
	lflist_t used_list;
#ifdef ENABLE_MSG_ERR_PROCESS
	lflist_t err_list;
#endif
	lflist_t free_pack_list;
	lflist_t used_pack_list;
};
#define bst_msg_queue_t struct _bst_msg_queue_t

int8_t msg_queue_init(bst_msg_queue_t *msg_queue);
int8_t msg_queue_in(bst_msg_queue_t *msg_queue, rw_msg_t *msg,
		    uint64_t timestamp);
int8_t msg_queue_out(bst_msg_queue_t *msg_queue, serdes_t *out);
int8_t rwmsg_queue_out(bst_msg_queue_t *msg_queue, rw_msg_t *out);
int8_t msg_queue_collate(bst_msg_queue_t *msg_queue, uint8_t is_err_handle);

#ifdef __cplusplus
}
#endif
#endif
